//
// Created by zhang on 3/23/2023.
//
#include <stdio.h>
#include <stdlib.h>

#define OVERFLOW -2
#define OK 0
#define ERROR -1
#define TRUE 1
#define FALSE 0

typedef int Status;
typedef int SElemType;
typedef int bool;


#define  MAXSIZE  100
typedef struct
{
    SElemType   *base;
    SElemType   *top;
    int stacksize;
} SqStack;

Status InitStack(SqStack *S) {
    S->base =malloc(sizeof(SElemType)*MAXSIZE);
    if(!S->base)
        return OVERFLOW;

    S->top = S->base;
    S->stacksize = MAXSIZE;

    return OK;
}

bool StackEmpty(SqStack S) {
    if(S.top == S.base)
        return TRUE;
    else return FALSE;
}

int StackLength(SqStack S) {
    return S.top-S.base;
}

Status ClearStack(SqStack S) {
    if(S.base) S.top = S.base;

    return OK;
}

Status DestroyStack(SqStack *S) {
    if(S->base)
    {
        free(S->base);
        S->stacksize = 0;
        S->base = S->top = NULL;
    }
    free(S);

    return OK;
}

Status Push(SqStack *S, SElemType e)
{
    if(S->top-S->base == S->stacksize) // 栈满
        return ERROR;
    *S->top=e;
    S->top++;
    return OK;
}

Status Pop(SqStack *S, SElemType *e) {
    if(S->top == S->base) // 栈空
        return ERROR;
    --S->top;
    *e=*S->top;
    return OK;
}

Status GetTop(SqStack S, SElemType *e)  { // peek
    if(S.top == S.base)	 return ERROR; 	// 栈空
    *e = *(S.top-1);

    return OK;
}

int main() {
    SqStack * stackp;
    InitStack(stackp);
    int i = 0;
    int val;

    for(i=0; i<10; i++) {
        Push(stackp, i);
    }

    for(i=0; i<10; i++) {
        Pop(stackp, &val);
        printf("%d: %d\n", i, val);
    }
    return 0;
}
